<?php

/*------------------------------------*\
	External Modules/Files
\*------------------------------------*/

// Load any external files you have here

/*------------------------------------*\
	Theme Support
\*------------------------------------*/

if (!isset($content_width))
{
	$content_width = 900;
}

if (function_exists('add_theme_support'))
{
	// Add Menu Support
	add_theme_support('menus');

	// Add Thumbnail Theme Support
	add_theme_support('post-thumbnails');
	add_image_size('large', 700, '', true); // Large Thumbnail
	add_image_size('medium', 250, '', true); // Medium Thumbnail
	add_image_size('small', 120, '', true); // Small Thumbnail
	add_image_size('custom-size', 700, 200, true); // Custom Thumbnail Size call using the_post_thumbnail('custom-size');

	// Add Support for Custom Backgrounds - Uncomment below if you're going to use
	/*add_theme_support('custom-background', array(
	'default-color' => 'FFF',
	'default-image' => get_template_directory_uri() . '/img/bg.jpg'
	));*/

	// Add Support for Custom Header - Uncomment below if you're going to use
	/*add_theme_support('custom-header', array(
	'default-image'			=> get_template_directory_uri() . '/img/headers/default.jpg',
	'header-text'			=> false,
	'default-text-color'		=> '000',
	'width'				=> 1000,
	'height'			=> 198,
	'random-default'		=> false,
	'wp-head-callback'		=> $wphead_cb,
	'admin-head-callback'		=> $adminhead_cb,
	'admin-preview-callback'	=> $adminpreview_cb
	));*/

	// Enables post and comment RSS feed links to head
	add_theme_support('automatic-feed-links');

	// Localisation Support
	load_theme_textdomain('html5blank', get_template_directory() . '/languages');
}

/*------------------------------------*\
	Functions
\*------------------------------------*/

function html5blank_header_scripts()
{
    if ($GLOBALS['pagenow'] != 'wp-login.php' && !is_admin()) {

    	wp_deregister_script('jquery'); // Deregister WordPress jQuery
    	wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array(), '1.9.1'); // Google CDN jQuery
    	wp_enqueue_script('jquery'); // Enqueue it!

    	wp_register_script('conditionizr', 'http://cdnjs.cloudflare.com/ajax/libs/conditionizr.js/4.0.0/conditionizr.js', array(), '4.0.0'); // Conditionizr
        wp_enqueue_script('conditionizr'); // Enqueue it!

        wp_register_script('modernizr', 'http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.7.1/modernizr.min.js', array(), '2.6.2'); // Modernizr
        wp_enqueue_script('modernizr'); // Enqueue it!
		
        wp_register_script('html5blankscripts', get_template_directory_uri() . '/js/scripts.js', array(), '1.0.0'); // Custom scripts
        wp_enqueue_script('html5blankscripts'); // Enqueue it!
        
        wp_register_script('fancybox', get_template_directory_uri() . '/js/fancybox/jquery.fancybox.js', array(), '1.0.0'); // Custom scripts
        wp_enqueue_script('fancybox'); // Enqueue it!
    }
}
add_action('init', 'html5blank_header_scripts'); // Add Custom Scripts to wp_head

// Pagination for paged posts, Page 1, Page 2, Page 3, with Next and Previous Links, No plugin
function html5wp_pagination()
{
	global $wp_query;
	$big = 999999999;
	echo "<div class=\"paginador\">".paginate_links(array(
		'base' => str_replace($big, '%#%', get_pagenum_link($big)),
		'format' => '?paged=%#%',
		'current' => max(1, get_query_var('paged')),
		'total' => $wp_query->max_num_pages,
		'prev_text'    => __('Anterior'),
		'next_text'    => __('Próximo'),
		'end_size' => 2,
		'mid_size' => 0,
	))."</div>";
}

function html5blank_styles()
{
    wp_register_style('normalize', get_template_directory_uri() . '/normalize.css', array(), '1.0', 'all');
    wp_enqueue_style('normalize'); // Enqueue it!

    wp_register_style('html5blank', get_template_directory_uri() . '/style.css', array(), '1.0', 'all');
    wp_enqueue_style('html5blank'); // Enqueue it!

    wp_register_style('fancybox', get_template_directory_uri() . '/js/fancybox/jquery.fancybox.css', array(), '1.0', 'all');
    wp_enqueue_style('fancybox'); // Enqueue it!
}
add_action('wp_enqueue_scripts', 'html5blank_styles'); // Add Theme Stylesheet

// function adepesc_scripts()
// {
// 	wp_enqueue_style( 'adepesc-style', get_stylesheet_uri() );
// 	wp_enqueue_script( 'adepesc-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131209', true );
// }
// add_action( 'wp_enqueue_scripts', 'adepesc_scripts' );

function nav_menu_first_last( $items )
{
	$position = strrpos($items, 'class="menu-item', -1);
	$items=substr_replace($items, 'menu-item-last ', $position+7, 0);
	$position = strpos($items, 'class="menu-item');
	$items=substr_replace($items, 'menu-item-first ', $position+7, 0);
	return $items;
}
add_filter( 'wp_nav_menu_items', 'nav_menu_first_last' );

if(!function_exists('sanatizeWord')) {
	function sanatizeWord($string){
		$acentos = array('À', 'Á', 'Ã', 'Â', 'à', 'á', 'ã', 'â', 'Ê', 'É', 'Í', 'í', 'Ó', 'Õ', 'Ô', 'ó', 'õ', 'ô', 'Ú', 'Ü', 'Ç', 'ç', 'é', 'ê', 'ú', 'ü', ' ', 'ª', 'º', '°', '/','(',')','|');
		$remove_acentos = array('a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'e', 'e', 'i', 'i', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'c', 'c', 'e', 'e', 'u', 'u', '-', '-', '-', '-', '-','-','-','-');
		return strtolower(str_replace($acentos, $remove_acentos, urldecode($string)));
	}
}
function special_nav_class($classes, $item){
	$classes[] = sanatizeWord($item->title);
	return $classes;
}
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);

if (function_exists('add_theme_support'))
{
    add_theme_support('Topo');
    add_theme_support('Rodapé');
}

function adepesc_widgets_init()
{
	require get_template_directory() . '/inc/widgets.php';
	// register_widget( 'Twenty_Fourteen_Ephemera_Widget' );

	register_sidebar( array(
		'name'          => __( 'Lateral direita', 'adepesc' ),
		'id'            => 'lateral-direita',
		'description'   => __( 'Barra lateral direita.', 'adepesc' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h1 class="widget-title">',
		'after_title'   => '</h1>',
	) );

	register_sidebar( array(
		'name'          => __( 'Miniaturas Home', 'adepesc' ),
		'id'            => 'miniatura-home',
		'description'   => __( 'Miniaturas da home.', 'adepesc' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h1 class="widget-title">',
		'after_title'   => '</h1>',
	) );
	
}
add_action( 'widgets_init', 'adepesc_widgets_init' );

function limitar($string, $max = 150, $end = "...")
{
    if(strlen($string) > $max){
	    $string = substr($string, 0, $max);
	    $pos    = strrpos($string, " ");//retorna o numero de caracteres que existem antes do ultimo espaço da string
	    $string = substr($string, 0, $pos);
	    $string .= $end;
    }
    return $string;
}

add_post_type_support('page','excerpt');

function get_the_excerpt_by_id($post_id) {
	global $post;
	$save_post = $post;
	$post = get_post($post_id);
	$output = get_the_excerpt();
	$post = $save_post;
	return $output;
}

add_theme_support( 'post-thumbnails' );
add_image_size( 'noticia-home', 315, 180, true );
add_image_size( 'noticia-destaque', 248, 158, true );
add_image_size( 'noticia-detalhe', 650, 295, true );
add_image_size( 'evento-lista', 274, 168, true );
add_image_size( 'banco-img-fb', 1000, 750, true );